package cn.lger.icollege.security;

import cn.lger.icollege.dao.TeacherDao;
import cn.lger.icollege.entity.Student;
import cn.lger.icollege.entity.Teacher;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * Code that Changed the World
 * Pro said
 * Created by Pro on 2018-04-21.
 */
@Component
public class TeacherUserDetailsService implements UserDetailsService {

    @Resource
    private TeacherDao teacherDao;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Optional<Teacher> optional = teacherDao.findById(username);
        if (optional.isPresent()) {
            return new CommonUserDetails(username, optional.get().getPassword(), "teacher",
                    AuthorityUtils.createAuthorityList("TEACHER"));
        }
        throw new UsernameNotFoundException("没有该教师");
    }
    
}
